package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.entity.BasicCarDetails;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.service.BasicCarDetailsService;
import com.ruicar.afs.cloud.basic.common.service.BasicMainInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xudongdong
 * @date 2020-06-16
 */
@Slf4j
@RestController
@RequestMapping("/custAssetInfo")
@AllArgsConstructor
public class BasicCarDetailsController {

    private BasicCarDetailsService basicCarDetailsService;
    private final BasicMainInfoService basicMainInfoService;


    @ApiOperation(value = "根据合同号码查询资产信息")
    @PostMapping("/getCustAssetInfo")
    public IResponse<BasicCarDetails> getCustAssetInfo(@RequestParam("contractNo") String contractNo) {
        List<BasicCarDetails> basicCarDetails = basicCarDetailsService.list(Wrappers.<BasicCarDetails>lambdaQuery().
                eq(BasicCarDetails::getContractNo, contractNo));
        BasicMainInfo basicMainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>lambdaQuery().
                eq(BasicMainInfo::getContractNo, contractNo));
        if (basicMainInfo != null && basicMainInfo.getCarNature() != null) {
            for (BasicCarDetails basicCarDeta : basicCarDetails) {
                basicCarDeta.setCarNature(basicMainInfo.getCarNature());
            }
        }

        return new IResponse().setData(basicCarDetails.get(0));
    }

    @ApiOperation(value = "根据合同号码查询资产信息（返回列表）")
    @PostMapping("/getCustAssetInfoList")
    public IResponse<List<BasicCarDetails>> getCustAssetInfoList(@RequestParam("contractNo") String contractNo) {
        List<BasicCarDetails> basicCarDetails = basicCarDetailsService.list(Wrappers.<BasicCarDetails>lambdaQuery().
                eq(BasicCarDetails::getContractNo, contractNo));
        return new IResponse().setData(basicCarDetails);
    }

}
